package com.one.controller;

import com.one.annotation.RequestPermission;
import com.one.annotation.RequestUser;
import com.one.bean.Ticket;
import com.one.constant.RedisPrefixConstant;
import com.one.constant.RequestReturnConstant;
import com.one.service.TicketService;
import com.one.util.RedisUtils;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * @author : zzh
  * @date : 2022/1/17
  * @return :
  * @description : 优惠券控制层
 */
@RestController
@Slf4j
@RequestMapping("/Ticket")
public class TicketController {


    @Autowired
    private TicketService ticketService;

    @Autowired
    private RedisUtils redisUtils;

    /**
      * @author : zzh
      * @date : 2022/1/17
      * @return :
      * @description : 查询所有优惠券
     */
    @GetMapping("/selectTicket")
    public RequestReturn<List<Ticket>> selectAllTicket(){
        List<Ticket> tickets = ticketService.selectTicket();
        return RequestReturnFactory.success(tickets);
    }

    /**
      * @author : zzh
      * @date : 2022/1/17
      * @return :
      * @description : 根据id查询优惠券
     */
    @GetMapping("/selectTicketById")
    public RequestReturn<Ticket> selectTicketById(@RequestParam Integer ticketId) {
        return RequestReturnFactory.success(ticketService.ticketById(ticketId));
    }

    /**
     * 用户兑换优惠券
     */
    @GetMapping("/coupons")
    @RequestPermission
    public RequestReturn<Map<String,Object>> userCoupons(@RequestUser UserVo userVo,@RequestParam Integer ticketId,@RequestHeader String token){
        Ticket ticket = ticketService.ticketById(ticketId);
//        Object userJson = redisUtils.get(token);
//        userVo = JSON.parseObject(userJson.toString(), UserVo.class);
        Map<String,Object> userTicket = new HashMap<String,Object>();

//        if (ticket.getId()==){
//            userTicket.put("原因","该优惠券不存在");
//            return RequestReturnFactory.fail(RequestReturnConstant.NOT_FOUND_CASH_COUPON_CODE,RequestReturnConstant.NOT_FOUND_CASH_COUPON_MSG,userTicket);
//        }

        if (userVo.getIntegral()<ticket.getIntegralNum()){
            userTicket.put("原因","用户积分不足");
            return RequestReturnFactory.fail(RequestReturnConstant.NOT_FOUND_CASH_COUPON_CODE,RequestReturnConstant.NOT_FOUND_CASH_COUPON_MSG,userTicket);
        }

        //用户剩余积分
        Integer integral1 = userVo.getIntegral()-ticket.getIntegralNum();
        userTicket.put("用户花费积分",ticket.getIntegralNum());
        userTicket.put("用户剩余积分",integral1);
        userTicket.put("兑换优惠券信息",ticket);
        userTicket.put("优惠券剩余时间","七天");
        userVo.setIntegral(integral1);
        redisUtils.set(token,userVo);
        //将用户兑换的优惠券存入redis中
        redisUtils.lSet(RedisPrefixConstant.TICKET_AUTO+userVo.getUserId(),ticketService.ticketById(ticketId));
        //获取用户优惠券redis下标
        long index = redisUtils.lGetListSize(RedisPrefixConstant.TICKET_AUTO + userVo.getUserId())-1;
        redisUtils.set(RedisPrefixConstant.TICKET_AUTO+userVo.getUserId()+":"+index,index,6480000);
        //写入优惠券过期前一天发送给用户的提醒
//        redisUtils.set(RedisPrefixConstant.TICKET_AUTO+userVo.getUserId()+":"+index+"remind","用户提醒",60*60*24*6);
        return RequestReturnFactory.success(userTicket);



//        if (ticketId==1&&userVo.getIntegral()>=50){
//            Integer integral1 = userVo.getIntegral()-50;
//            userTicket.put("用户花费积分",50);
//            userTicket.put("用户剩余积分",integral1);
//            userTicket.put("兑换优惠券信息",ticket);
//            userVo.setIntegral(integral1);
//            redisUtils.set(token,userVo);
//            log.info("------id为{}的用户购买一号现金券成功,剩余积分:{}------",userVo.getUserId(),integral1);
//            redisUtils.lSet(userVo.getUserId()+"号卡包",ticketService.ticketById(ticketId),604800);
//            return RequestReturnFactory.success(userTicket);
//        }else if(ticketId==2&&userVo.getIntegral()>=100) {
//            Integer integral1 = userVo.getIntegral() - 100;
//            userVo.setIntegral(integral1);
//            redisUtils.set(token, userVo);
//            log.info("------id为{}的用户购买一号现金券成功,剩余积分:{}------",userVo.getUserId(),integral1);
//            redisUtils.lSet(userVo.getUserId()+"号卡包",ticketService.ticketById(ticketId), 604800);
//            return RequestReturnFactory.success(userTicket);
//        }else if (ticketId==3&&userVo.getIntegral()>=200){
//            Integer integral1 = userVo.getIntegral() - 200;
//            userVo.setIntegral(integral1);
//            redisUtils.set(token, userVo);
//            log.info("------id为{}的用户购买一号现金券成功,剩余积分:{}------",userVo.getUserId(),integral1);
//            redisUtils.lSet(userVo.getUserId()+"号卡包",ticketService.ticketById(ticketId), 604800);
//            return RequestReturnFactory.success(userTicket);
//        }else if (ticketId!=3|){
//            return RequestReturnFactory.fail(userTicket);
//        }
//        return RequestReturnFactory.success();
    }

    /**
      * @author : zzh
      * @date : 2022/1/18
      * @return :
      * @description : 用户查看拥有优惠券
     */
    @GetMapping("selectUserTicket")
    @RequestPermission
    public RequestReturn<HashMap<String, Object>> selectUserTicket(@RequestUser UserVo userVo, @RequestHeader String token){
        HashMap<String, Object> returnMap = new HashMap<>();
        //判断用户信息中是否存在优惠券
        if (!redisUtils.hasKey(RedisPrefixConstant.TICKET_AUTO+userVo.getUserId())){
            returnMap.put(RequestReturnConstant.USER_TICKET_MSG,"暂无现金券~,快去兑换券购物吧");
            return RequestReturnFactory.success(returnMap);
        }
            returnMap.put(RequestReturnConstant.USER_TICKET_MSG,redisUtils.lGet(RedisPrefixConstant.TICKET_AUTO+userVo.getUserId(),0,-1));
           return RequestReturnFactory.success(returnMap);
    }




}
